package com.icinfo.cloud.provider.peripheralinterface.xfjdzf.service.impl;

import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.utils.HttpUtils;
import com.icinfo.cloud.provider.peripheralinterface.xfjdzf.dto.XfjdzfDocumentNumberInfoDto;
import com.icinfo.cloud.provider.peripheralinterface.xfjdzf.enums.XfjdzfDocumentTypeReflectEnum;
import com.icinfo.cloud.provider.peripheralinterface.xfjdzf.service.IXfjdzfInterfaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：消防监督执法接口服务实现类
 *
 * @author xiangyuyu
 * @date 2022/8/13
 * @since 1.3.2
 */
@Service
@Slf4j
public class XfjdzfInterfaceServiceImpl implements IXfjdzfInterfaceService {
    @Value("${xfjdzf.getDocumentNumberUrl}")
    private String getXfjdzfDocumentNumberUrl;

    /**
     * 描述：【V1.3.2】描述：获取消防监督执法系统文书文号信息
     *
     * @param dto {@link XfjdzfDocumentNumberInfoDto}
     * @return String 文书文号信息
     * @author xiangyuyu
     * @date 2022/8/13
     * @since 1.3.2
     */
    @Override
    public String getDocumentNumber(XfjdzfDocumentNumberInfoDto dto) {
        log.info("getDocumentNumber start...dto:{}", dto);
        String documentNumber;
        String orgCode = dto.getOrgCode();
        String documentType = dto.getDocumentType();
        if (!XfjdzfDocumentTypeReflectEnum.isLegalEnumCode(documentType)) {
            throw new BusinessException("非法映射文书种类");
        }
        String transferDocumentType = XfjdzfDocumentTypeReflectEnum.getValueByCode(documentType);
        try {
            //String assembleUrl = getXfjdzfDocumentNumberUrl + "?orgCode=" + orgCode + "&bookConfigNo=" + transferDocumentType;
            //log.info("assembleUrl:{}", assembleUrl);
            Map<String, String> paramMap = new HashMap<>(2);
            paramMap.put("orgCode", orgCode);
            paramMap.put("bookConfigNo", transferDocumentType);

            //http://112.124.244.7/inout/app/bookCommon/getBookDocNo
            getXfjdzfDocumentNumberUrl = "http://10.145.147.112:8581/proxy/xfjdzfDocNumber/inout/app/bookCommon/getBookDocNo";
            String result = HttpUtils.doGet(getXfjdzfDocumentNumberUrl, paramMap);
            log.info("getDocumentNumber result:{}", result);
            if (!ObjectUtils.isEmpty(result)) {
                JSONObject resultJO = JSONObject.parseObject(result);
                if (!ObjectUtils.isEmpty(resultJO)) {
                    //先判断code
                    Object codeObj = resultJO.get(Constant.STRING_CODE_LOWER);
                    if (ObjectUtils.isEmpty(codeObj)) {
                        log.warn("getDocumentNumber 相应code为null,msg:{}", resultJO.getString(Constant.STRING_MSG_LOWER));
                        throw new BusinessException("getDocumentNumber 相应code为null,msg:" + resultJO.getString(Constant.STRING_MSG_LOWER));
                    }
                    if (HttpStatus.HTTP_OK == (int) codeObj) {
                        Object data = resultJO.get(Constant.STRING_DATA_LOWER);
                        if (ObjectUtils.isEmpty(data)) {
                            log.warn("getDocumentNumber 相应data为null,msg:{}", resultJO.getString(Constant.STRING_MSG_LOWER));
                            throw new BusinessException("getDocumentNumber 相应data为null,msg:" + resultJO.getString(Constant.STRING_MSG_LOWER));
                        } else {
                            documentNumber = (String)data;
                        }
                    } else {
                        log.warn("getDocumentNumber error,msg:{}", resultJO.getString(Constant.STRING_MESSAGE_LOWER));
                        throw new BusinessException("getDocumentNumber error,msg:" + resultJO.getString(Constant.STRING_MESSAGE_LOWER));
                    }
                } else {
                    log.warn("getDocumentNumber error(result transform JSONObject)");
                    throw new BusinessException("getDocumentNumber error(result transform JSONObject)");
                }
            } else {
                log.warn("getDocumentNumber result:null");
                throw new BusinessException("getDocumentNumber result:null");
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.warn("getDocumentNumber error:{}", e.getMessage());
            throw new BusinessException("getDocumentNumber error:" + e.getMessage());
        }
        log.info("getDocumentNumber end documentNumber:", documentNumber);
        return documentNumber;
    }
}
